package com.project.collectAPI.config;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;




//@Configuration
@WebFilter(filterName = "APPtest",urlPatterns = "/*")//拦截全部
public class APPtest implements Filter  {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //System.out.println("过滤代码");
        //ServletResponse responseWrapper=new HttpServletResponseWrapper((HttpServletResponse)servletResponse);
        /*HttpServletResponseWrapper responseWrapper=new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
        HttpServletRequestWrapper requestWrapper=new HttpServletRequestWrapper((HttpServletRequest) servletRequest);
        String content=requestWrapper.getParameter("form").substring(0,100);
        System.out.println(content);

        //writeResponse(servletResponse,content);
        filterChain.doFilter(requestWrapper, responseWrapper);*/

        System.out.println("进入doFilter过滤请求.....");
        ParameterRequestWrapper parmsRequest = new ParameterRequestWrapper((HttpServletRequest) servletRequest);
        filterChain.doFilter(parmsRequest, servletResponse);
    }

    public static void writeResponse(HttpServletResponse response, String json) {
        try {
            response.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
